<?php

class WPUF_Edit_Post {
    function __construct() {
        add_shortcode( 'wpuf_edit', array($this, 'shortcode') );
    }
    /**
     * Handles the edit post shortcode
     *
     * @return string generated form by the plugin
     */
    function shortcode() {
        ob_start();
		// USER MUST BE LOGGED IN OR HAVE AN ACCESS KEY BEFORE WE CONTINUE
			if ( is_user_logged_in() ) {
				$this->prepare_form();
			} elseif ( isset($_GET['access']) && get_post_meta( $_GET['pid'], 'accessKey', true ) == $_GET['access'] ) {
				$creds = array( 'user_login' => 'AccessKeyUser', 'user_password' => 'UploadsDontWorkForAccessUsersNotSignedIn', 'remember' => false );
				$user = wp_signon( $creds, false );
				if ( !is_user_logged_in() ) { // have to refresh the page again to get the temp login to work for some reason
					wp_redirect(curPageURL());
				}
				$this->prepare_form();
			} else {
				printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( '', false ) );
			}
			$content = ob_get_contents();
			ob_end_clean();
			return $content;
    }
    /**
     * Main edit post form
     *
     * @global type $wpdb
     * @global type $userdata
     */
    function prepare_form() {
		// GET INFO
			global $wpdb, $current_user;
			$post_id = isset( $_GET['pid'] ) ? intval( $_GET['pid'] ) : 0;
			$curpost = get_post( $post_id );
		// IF BAD POST ID
			if ( !$curpost ) {
				echo 'Invalid Listing';
				return;
			}
		// GET MORE INFO
			$ssaId = $curpost->post_author;
			$apID = get_post_meta( $post_id, 'apID', true );
			$accessKey = get_post_meta( $post_id, 'accessKey', true );
        // has permission? MUST BE THE CORRECT SSA OR AP OR ACCESS KEY USER
			if ( $current_user->ID != $ssaId && $current_user->ID != $apID && $accessKey != $_GET['access'] && !is_god() ) {
				echo 'You are not allowed to edit this listing.';
				return;
			}
        // perform delete attachment action
			if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "del" ) {
				check_admin_referer( 'wpuf_attach_del' );
				$attach_id = intval( $_REQUEST['attach_id'] );
				if ( $attach_id ) {
					wp_delete_attachment( $attach_id );
				}
			}
        // process post
			if ( isset( $_POST['wpuf_edit_post_submit'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpuf-edit-post' ) ) {
				$this->submit_post();
			}
        // show post form
			$this->edit_form( $curpost );
    }
	
    function edit_form( $curpost ) {
		// GET THE SSA / AP INFO
			$ssaID = $curpost->post_author;
			$ssa = get_user_by('id', $ssaID);
			$apID = get_post_meta( $curpost->ID, 'apID', true );
			$ap = get_user_by('id', $apID);

       ?>
        <div id="wpuf-post-area">
        
        
        
            <form name="wpuf_edit_post_form" id="wpuf_edit_post_form" action="" enctype="multipart/form-data" method="POST">
            
                <?php wp_nonce_field( 'wpuf-edit-post' ); ?>
                
				<?php
                    // GET AND DISPLAY LISTING INFO
                        $apEmail = get_post_meta( $curpost->ID, 'apEmail', true );
                        $apPhone = get_post_meta( $curpost->ID, 'apPhone', true );
                        $ListingAddress = get_post_meta( $curpost->ID, 'ListingAddress', true );
                        $ListingPrice = get_post_meta( $curpost->ID, 'ListingPrice', true );
                        $ListingDate = get_post_meta( $curpost->ID, 'ListingDate', true );
                        $ListingExpirationDate = get_post_meta( $curpost->ID, 'ListingExpirationDate', true );
                        $EquatorUserName = get_post_meta( $curpost->ID, 'EquatorUserName', true );
                        $EquatorPassword = get_post_meta( $curpost->ID, 'EquatorPassword', true );
                        $titleCompany = get_post_meta( $curpost->ID, 'titleCompany', true );
                        $titleCoContact = get_post_meta( $curpost->ID, 'titleCoContact', true );
                        $titleCoPhone = get_post_meta( $curpost->ID, 'titleCoPhone', true );
                        $titleCoEmail = get_post_meta( $curpost->ID, 'titleCoEmail', true );
                        $brokerName = get_post_meta( $curpost->ID, 'brokerName', true );
						$listingStatus = get_post_meta( $curpost->ID, 'listingStatus', true );
						$documentTracking = get_post_meta( $curpost->ID, 'documentTracking', true );
						echo $documentTracking;
                ?>
                        
                <h1>Listing Submission Info:</h1>
                
				<?php if ( is_ap() ) { 
                    echo '<label>Short Sale Assistant:</label><input type="text" disabled="disabled" value="' . $ssa->display_name . '" /><br />';
                } else {
                    echo '<label>Agent Partner:</label><input type="text" disabled="disabled" value="' . $ap->display_name . '" /><br />';
                } ?>
                 
                <?php if ( !isset($_GET['access']) ) { ?>     
                    <label>Your Email: * </label><input type="text" name="apEmail" value="<?php if ( isset($_POST['apEmail']) ) { echo $_POST['apEmail']; } else { echo $apEmail; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
                    <label>Your Phone: * </label><input type="text" name="apPhone" value="<?php if ( isset($_POST['apPhone']) ) { echo $_POST['apPhone']; } else { echo $apPhone; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
                <?php } ?>
                
                <label>Listing Address: * </label><input type="text" name="ListingAddress" value="<?php if ( isset($_POST['ListingAddress']) ) { echo $_POST['ListingAddress']; } else { echo $ListingAddress; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
               
                <?php if ( !isset($_GET['access']) ) { ?>     
                    <label>Listing Price: * </label><input type="text" name="ListingPrice" value="<?php if ( isset($_POST['ListingPrice']) ) { echo $_POST['ListingPrice']; } else { echo $ListingPrice; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
                    <label>Listing Date: * </label><input type="text" name="ListingDate" value="<?php if ( isset($_POST['ListingDate']) ) { echo $_POST['ListingDate']; } else { echo $ListingDate; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
                    <label>Listing Expiration Date: * </label><input type="text" name="ListingExpirationDate" value="<?php if ( isset($_POST['ListingExpirationDate']) ) { echo $_POST['ListingExpirationDate']; } else { echo $ListingExpirationDate; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
                    <?php if ( $listingStatus != 'Closed' ) { ?>
                        <label>Equator UserName: </label><input type="text" name="EquatorUserName" value="<?php if ( isset($_POST['EquatorUserName']) ) { echo $_POST['EquatorUserName']; } else { echo $EquatorUserName; } ?>" <?php if ( isset($_GET['access']) ) { echo 'disabled="disabled" '; } ?>/><br />
                        <label>Equator Password: </label><input type="text" name="EquatorPassword" value="<?php if ( isset($_POST['EquatorPassword']) ) { echo $_POST['EquatorPassword']; } else { echo $EquatorPassword; } ?>" <?php if ( isset($_GET['access']) ) { echo 'disabled="disabled" '; } ?>/><br />
					<?php } ?>
                        <label>Title Company Name: </label><input type="text" name="titleCompany" value="<?php if ( isset($_POST['titleCompany']) ) { echo $_POST['titleCompany']; } else { echo $titleCompany; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br />
                    <?php if ( $listingStatus != 'Closed' ) { ?>
                        <label>Title Company Contact: </label><input type="text" name="titleCoContact" value="<?php if ( isset($_POST['titleCoContact']) ) { echo $_POST['titleCoContact']; } else { echo $titleCoContact; } ?>" <?php if ( isset($_GET['access']) ) { echo 'disabled="disabled" '; } ?>/><br />
                        <label>Title Company Phone: </label><input type="text" name="titleCoPhone" value="<?php if ( isset($_POST['titleCoPhone']) ) { echo $_POST['titleCoPhone']; } else { echo $titleCoPhone; } ?>" <?php if ( isset($_GET['access']) ) { echo 'disabled="disabled" '; } ?>/><br />
                        <label>Title Company Email: </label><input type="text" name="titleCoEmail" value="<?php if ( isset($_POST['titleCoEmail']) ) { echo $_POST['titleCoEmail']; } else { echo $titleCoEmail; } ?>" <?php if ( isset($_GET['access']) ) { echo 'disabled="disabled" '; } ?>/><br />
					<?php } ?>
                        <label>Brokerage Name: * </label><input type="text" name="brokerName" value="<?php if ( isset($_POST['brokerName']) ) { echo $_POST['brokerName']; } else { echo $brokerName; } ?>" <?php if ( isset($_GET['access']) || $listingStatus == 'Closed' ) { echo 'disabled="disabled" '; } ?>/><br /><br />
                <?php } ?>
                
                <?php if ( !isset($_GET['access']) ) { ?>     
                    <h1>Listing Status:</h1>
                        <label>Status: * </label>
                        <select name="listingStatus"<?php if ( !is_ap() && !is_ssa() && !is_god() ) { echo ' disabled="disabled" '; } ?>>
                            <option value="Listing submitted"<?php if ( $listingStatus == 'Listing submitted' ) { echo ' selected="selected"'; } ?>>Listing submitted</option>
                            <option value="Offer submitted"<?php if ( $listingStatus == 'Offer submitted' ) { echo ' selected="selected"'; } ?>>Offer submitted</option>
                            <option value="Closed"<?php if ( $listingStatus == 'Closed' ) { echo ' selected="selected"'; } ?>>Closed</option>
                        </select>
                        <a href="<?php echo site_url(); ?>/wp-content/themes/master/toolTips/listingStatus.html?width=375" class="jTip" id="two" name="Listing Status:">?</a>
                        <br /><br />
                <? } ?>
                
                <?php if ( is_ap() && $listingStatus != 'Closed' ) { ?>
                    <h1>Document Tracking:</h1>
                        <label>Tracking: * </label>
                        <select name="documentTracking"<?php if ( !is_ap() && !is_god() ) { echo ' disabled="disabled" '; } ?>>
                            <option value="autoTracking"<?php if ( $documentTracking == 'autoTracking' ) { echo ' selected="selected"'; } ?>>Email Tracking</option>
                            <option value="manualTracking"<?php if ( $documentTracking == 'manualTracking' ) { echo ' selected="selected"'; } ?>>Normal Tracking</option>
                            <option value="noTracking"<?php if ( $documentTracking == 'noTracking' ) { echo ' selected="selected"'; } ?>>No Tracking</option>
                        </select>
                        <a href="<?php echo site_url(); ?>/wp-content/themes/master/toolTips/documentTracking.html?width=375" class="jTip" id="three" name="Document Tracking:">?</a>
                        <br />
                <? } ?>

                
				<?php if ( $listingStatus != 'Closed' ) { // IS LISTING OPEN?
                    echo '<p>';
                        echo '<br /><h1>Documents needed to process your shortsale:</h1>';
                        echo '<ul class="docList">';
                            echo '<li><img src="' . get_bloginfo( 'template_directory' ) . '/images/docsGreenCheck.png" width="20" height="20" /><span>= Submitted document</span><br /></li>';
                            if ( $documentTracking == 'autoTracking' &&  !isset($_GET['access']) ) { echo '<li><img src="' . get_bloginfo( 'template_directory' ) . '/images/docsRedX.png" width="20" height="20" /><span>= Required document missing</span><br /></li>'; }
                            echo '<li><img src="' . get_bloginfo( 'template_directory' ) . '/images/docsDownload.png" width="20" height="20"><span>= Blank copy of document, click arrow to download</span><br /><br /></li>';
                        echo '</ul>';
                    echo '</p>';
                
                
					// OUTPUT LIST OF REQUIRED DOCS NOT YET SUBMITTED
						if ( isset($_GET['access']) ) {
							$docTypesArray = array( 'Homeowner', 'Lender' ); // ONLY SHOW LIMITED DOCS FOR ACCESS KEYS
						} else {
							$docTypesArray = docTypesArray(); // ELSE SHOW ALL DOCS
						}
						echo '<ul>';
							$count = 0;
							foreach ( $docTypesArray as $docType ) {
								$count++;
								echo '<li class="docTypeGroup">';
									echo '<span class="docTypeGroupTitle">' . $docType . ' Docs</span>';
									echo '<ul class="docList">';
										foreach ( docsArray($docType) as $doc ) {
											echo '<li>';
												if ( get_post_meta($curpost->ID, $doc['id'], true) ) {
													echo '<img src="' . get_bloginfo( 'template_directory' ) . '/images/docsGreenCheck.png" width="20" height="20" />'; // SHOW GREEN CHECK
												} elseif ( $doc['required'] &&  !isset($_GET['access']) && $documentTracking != 'noTracking' ) { // RED X ONLY IF DOCUMENT TRACKING IS ENABLED / DONT SHOW MISSING DOCS TO ACCESS KEY USERS EITHER
													echo '<img src="' . get_bloginfo( 'template_directory' ) . '/images/docsRedX.png" width="20" height="20" />'; // SHOW RED X
												} else {
													echo '<span style="width:25px;"></span>'; // SHOW A SPACER
												}
												echo '<span>' . $doc['label'] . '</span>';
												if ( $doc['downloadURL'] ) { echo '<a href="http://trudocs.com/wp-content/downloads/' . $doc['downloadURL'] . '" class="docsDownload"><img src="' . get_bloginfo( 'template_directory' ) . '/images/docsDownload.png" width="15" height="15" /></a>'; } // SHOW DOWNLOAD ICON
												echo '<div style="clear:both;"></div>';
											echo '</li>';
										}
									echo '</ul>';
								echo '</li>';
								if ( $count == 2 ) { echo '<div style="clear:both;"></div>'; $count = 0; }
							}
						echo '</ul>';
				}  // END: IS DOC TRACKING ENABLED? IS LISTING OPEN? ?>
                
                <div style="clear:both;"></div>
                
				<?php if ( $listingStatus != 'Closed' ) { // IS LISTING OPEN? ?>
                    <h2 style="margin-top:25px;">You may Fax, Email OR upload your documents.</h2>
                    <p style="margin-bottom:25px;"><strong>Fax:</strong> 1-800-918-7028<br />
                    <strong>Email:</strong> <?php echo $ssa->user_email; ?></p>
                <?php } ?>
            
				<?php
					if ( $listingStatus != 'Closed' ) { // IS LISTING OPEN?
						do_action( 'wpuf_add_post_form_tags', $curpost->post_type, $curpost ); // DOCUMENT UPLOAD
					}
				?>
                
                <input class="wpuf_submit" type="submit" name="wpuf_edit_post_submit" value="Update Listing">
                <input type="hidden" name="wpuf_edit_post_submit" value="yes" />
                <input type="hidden" name="post_id" value="<?php echo $curpost->ID; ?>">
                
            </form>
            
        </div>

        <?php
    }

    function submit_post() {
		
        // GET POST ID, SSA ID/INFO, and CURRENT USER Global
			$post_id = $_POST['post_id'];
			$curpost = get_post( $post_id );
			$ssaID = $curpost->post_author;
			$ssa = get_user_by('id', $ssaID);
			global $current_user;
			
		// GATHER OUR INFO
			$prevListingStatus = get_post_meta( $post_id, 'listingStatus', true );
			$listingStatus = $_POST['listingStatus'];
			$errors = array();
			if ( $prevListingStatus == 'Closed' || !is_guest() || $listingStatus != 'Listing submitted' ||  $listingStatus != 'Offer submitted' ) { // ACCESS KEY USER OR CLOSED LISTING, JUST GRAB PREVIOUS VALUES 
				$apEmail = get_post_meta( $post_id, 'apEmail', true );
				$apPhone = get_post_meta( $post_id, 'apPhone', true );
				$ListingAddress = get_post_meta( $post_id, 'ListingAddress', true );
				$ListingPrice = get_post_meta( $post_id, 'ListingPrice', true );
				$ListingDate = get_post_meta( $post_id, 'ListingDate', true );
				$ListingExpirationDate = get_post_meta( $post_id, 'ListingExpirationDate', true );
				$EquatorUserName = get_post_meta( $post_id, 'EquatorUserName', true );
				$EquatorPassword = get_post_meta( $post_id, 'EquatorPassword', true );
				$titleCompany = get_post_meta( $post_id, 'titleCompany', true );
				$titleCoContact = get_post_meta( $post_id, 'titleCoContact', true );
				$titleCoPhone = get_post_meta( $post_id, 'titleCoPhone', true );
				$titleCoEmail = get_post_meta( $post_id, 'titleCoEmail', true );
				$brokerName = get_post_meta( $post_id, 'brokerName', true );
				//if ( $listingStatus == '' ) { $listingStatus = get_post_meta( $post_id, 'listingStatus', true ); } // ON CLOSED LISTINGS BEING UPDATED TO OPEN
				$documentTracking = get_post_meta( $post_id, 'documentTracking', true );
				//echo $documentTracking;
			} else { // LISTING IS OPEN & NOT BEING REOPENED & NOT ACCESS KEY USER
				if ( $_POST['apEmail'] != '' ) { $apEmail = $_POST['apEmail']; } else { $errors[] = 'Please Provide Your Email'; }
				if ( $_POST['apPhone'] != '' ) { $apPhone = $_POST['apPhone']; } else { $errors[] = 'Please Provide Your Phone Number.'; }
				if ( $_POST['ListingAddress'] != '' ) { $ListingAddress = $_POST['ListingAddress']; } else { $errors[] = 'Please Provide Listing Address'; }
				if ( $_POST['ListingPrice'] != '' ) { $ListingPrice = $_POST['ListingPrice']; } else { $errors[] = 'Please Provide Listing Price'; }
				if ( $_POST['ListingDate'] != '' ) { $ListingDate = $_POST['ListingDate']; } else { $errors[] = 'Please Provide Listing Date'; }
				if ( $_POST['ListingExpirationDate'] != '' ) { $ListingExpirationDate = $_POST['ListingExpirationDate']; } else { $errors[] = 'Please Provide Listing Expiration Date'; }
				if ( $_POST['brokerName'] != '' ) { $brokerName = $_POST['brokerName']; } else { $errors[] = 'Please Provide Brokerage Name '; }
				$EquatorUserName = $_POST['EquatorUserName'];
				$EquatorPassword = $_POST['EquatorPassword'];
				$titleCompany = $_POST['titleCompany'];
				$titleCoContact = $_POST['titleCoContact'];
				$titleCoPhone = $_POST['titleCoPhone'];
				$titleCoEmail = $_POST['titleCoEmail'];
				$documentTracking = $_POST['documentTracking'];
				//if ( $documentTracking == '' ) { $documentTracking = get_post_meta( $post_id, 'documentTracking', true ); } // ON CLOSED LISTINGS BEING UPDATED TO OPEN,  $_POST['documentTracking'] IS NOT AVAILABLE - GRAB PREVIOUS VALUE
			}

		// SUCCESS - NO ERRORS
			if ( empty($errors) ) {
					
				// ACTION TO HOOK ATTACHMENT FUNCTIONS
					do_action( 'wpuf_edit_post_after_update', $post_id );
				
				// UPDATE LISTING AND META INFO
					$postInfo = array(
						'ID'    => $post_id,
						'post_title'     => $ListingAddress,
					);
					wp_insert_post($postInfo);
					if ( !isset($_GET['access']) ) {
						update_post_meta( $post_id, 'apEmail', $apEmail );
						update_post_meta( $post_id, 'apPhone', $apPhone );
						update_post_meta( $post_id, 'ListingAddress', $ListingAddress );
						update_post_meta( $post_id, 'ListingPrice', $ListingPrice );
						update_post_meta( $post_id, 'ListingDate', $ListingDate );
						update_post_meta( $post_id, 'ListingExpirationDate', $ListingExpirationDate );
						update_post_meta( $post_id, 'EquatorUserName', $EquatorUserName );
						update_post_meta( $post_id, 'EquatorPassword', $EquatorPassword );
						update_post_meta( $post_id, 'titleCompany', $titleCompany );
						update_post_meta( $post_id, 'titleCoContact', $titleCoContact );
						update_post_meta( $post_id, 'titleCoPhone', $titleCoPhone );
						update_post_meta( $post_id, 'titleCoEmail', $titleCoEmail );
						update_post_meta( $post_id, 'brokerName', $brokerName );
						update_post_meta( $post_id, 'listingStatus', $listingStatus );
						update_post_meta( $post_id, 'documentTracking', $documentTracking );
						update_post_meta( $post_id, 'listingUpdated', time() );
					}
					echo $documentTracking;
		
				// PROCESS ATTACHED DOCUMENTS
					$args = array(
						'post_type' => 'attachment',
						'numberposts' => -1,
						'post_status' => null,
						'post_parent' => $post_id
					); 
					$attachments = get_posts($args);
					if ($attachments) {
						// RESET ALL TO FALSE FIRST SO WE HAVE AN ACCURATE LIST OF SUBMMITTED DOCS.
							foreach ( docTypesArray() as $docType ) {
									foreach ( docsArray($docType) as $doc ) {
										delete_post_meta( $post_id, $doc['id'] );
									}
							}
						// NOW THAT OUR SLATE IS CLEAN, UPDATE WHICH DOCS HAVE BENN SUBMITTED
							foreach ( $attachments as $attachment ) {
								$attachedDoc = 'Doc' . $attachment->ID;
								$whichDoc = $_POST[$attachedDoc];
								if ( $whichDoc != '' ) {
									update_post_meta( $attachment->ID, 'whichDoc', $whichDoc ); // UPDATE EACH ATTACHMENT POST META WITH WHAT DOC THE ATTACHMENT IS
									update_post_meta( $post_id, $whichDoc, true ); // UPDATE THE PARENT POST ( THE LISTING ) META THAT THIS DOC HAS BEEN SUBMITTED
								}
							}
						 // SET REQUIRED DOCS MISSING STATUS
							$reqDocsMissing = false;
							foreach ( docTypesArray() as $docType ) {
									foreach ( docsArray($docType) as $doc ) {
										// IF DOC IS REQUIRED BUT NOT YET SUBMITTED
										if ( $doc['required'] && get_post_meta($post_id, $doc['id'], true) == '' ) {
											$reqDocsMissing = true; // UPDATE POSTMETA -> REQ DOC IS MISSING
										}
									}
							}
							if ( $documentTracking != 'noTracking' ) { // IS DOC TRACKING ENABLED?
								update_post_meta( $post_id, 'reqDocsMissing', $reqDocsMissing ); // UPDATE POSTMETA REQ DOCS MISSING?
							} else { // NO DOC TRACKING
								update_post_meta( $post_id, 'reqDocsMissing', false ); // UPDATE POSTMETA REQ DOCS MISSING
							}
					}
				
				// EMAIL TO SSA
					$message = "Listing Submission Info: \r\r" . 
					"Name: " . $current_user->user_firstname . " " . $current_user->user_lastname . " \r" . 
					"Email: "  . $apEmail . " \r" . 
					"Phone: "  . $apPhone . " \r" . 
					"Listing Address: "  . $ListingAddress . " \r" . 
					"Listing Price: "  . $ListingPrice . " \r" . 
					"Listing Date: "  . $ListingDate . " \r" . 
					"Listing Expiration Date: "  . $ListingExpirationDate . " \r" . 
					"Equator Username: "  . $EquatorUserName . " \r" . 
					"Equator Password: "  . $EquatorPassword . " \r" . 
					"Title Company Name: "  . $titleCompany . " \r" . 
					"Title Company Contact: "  . $titleCoContact . " \r" . 
					"Title Company Phone: "  . $titleCoPhone . " \r" . 
					"Title Company Email: "  . $titleCoEmail . " \r";
					"Brokerage Name: "  . $brokerName . " \r\r" . 			
					"Submitted Documents List: \r\r";
					$attachmentPaths = array();
					if ( $attachments ) { // WE FETCHED THESE ABOVE
						foreach ( $attachments as $attachment ) {
							$message .= str_replace( '/home/webadmin/trudocs.com/html', 'http://trudocs.com', get_attached_file($attachment->ID) ) . "\r"; // FOR DOC LIST IN EMAIL CONTENT
							$attachmentPaths[] = get_attached_file($attachment->ID); // FOR EMAIL ATTACHMENTS
						}
					}
					$headers[] = 'From: [trudocs] <submissions@trudocs.com>';
					$headers[] = 'Bcc: submissions@trudocs.com';
					wp_mail($ssa->user_email, 'Listing Updated - ' . $current_user->user_firstname . " " . $current_user->user_lastname . ' - ' . $ListingAddress, $message, $headers, $attachmentPaths);
					
				// ECHO SUCCESS
					echo '<div class="success">Listing Updated Succesfully!</div>';
				
			} else { // FAILURE
			
				// ECHO ERRORS
					echo '<div class="error">Incomplete Listing Information:</div>';
					echo '<ul>';
						foreach ( $errors as $error ) {
							echo '<li style="color:red;">' . $error . '...</li>';
						}
					echo '</ul>';
								
			} // END VALIDATION - IF (errors) {

	} // END function submit_post()

} // END CLASS

$wpuf_edit = new WPUF_Edit_Post();